package com.example.apidocserver.domain.document.event.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.example.apidocserver.domain.document.dto.MenuApiNodeDto;
import com.example.apidocserver.domain.document.dto.UploadMenuApiDto;
import com.example.apidocserver.domain.document.entity.DocMenu;
import com.example.apidocserver.domain.document.event.DocLoadDomainEvent;
import com.example.apidocserver.infrastructure.config.security.UserDetailsImpl;
import com.example.apidocserver.infrastructure.constants.SystemConstants;
import com.example.apidocserver.infrastructure.util.user.UserUtil;
import com.example.apidocserver.infrastructure.util.document.TalendJsonToEntity;
import com.example.apidocserver.infrastructure.util.result.ParamException;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Optional;

import static com.example.apidocserver.infrastructure.constants.TalendJsonConstants.*;

/**
 * 文档上传下载
 *
 * @author xyqlg
 */
@Service
public class DocLoadDomainEventImpl implements DocLoadDomainEvent {
    @Override
    public UploadMenuApiDto jonsToEntities(MultipartFile file, Integer spaceId, Integer docId) throws IOException {
        byte[] bytes = file.getBytes();
        JSONObject jsonObject = JSONObject.parseObject(new String(bytes));
        JSONArray paramInArr = jsonObject.getJSONArray("entities");
        if (CollectionUtils.isEmpty(paramInArr)) {
            throw new ParamException("导入数据无效。");
        }

        // 1.解析JSON
        UploadMenuApiDto menuApiDto = new UploadMenuApiDto();
        Optional<UserDetailsImpl> userDetails = UserUtil.getUserDetails();
        userDetails.ifPresent(details -> menuApiDto.setUserId(Math.toIntExact(details.getUserId())));
        DocMenu docMenuTop = new DocMenu().setId(SystemConstants.DATA_COMMON_ID).setIdPath("");
        MenuApiNodeDto nodeTop = MenuApiNodeDto.builder().node(docMenuTop).build();
        uploadMenuNodeFromJsonArray(paramInArr, menuApiDto, nodeTop, spaceId, docId);
        return menuApiDto;
    }

    private void uploadMenuNodeFromJsonArray(JSONArray paramInArr, UploadMenuApiDto menuApiDto, MenuApiNodeDto nodeParent,
                                             Integer spaceId, Integer docId) {
        if (CollectionUtils.isEmpty(paramInArr)) {
            return;
        }
        for (int i = 0; i < paramInArr.size(); i++) {
            JSONObject param = paramInArr.getJSONObject(i);
            JSONObject entity = param.getJSONObject(FS_ENTITY);
            // 目录信息、创建节点对象
            MenuApiNodeDto node = TalendJsonToEntity.getNodeSetMenu(menuApiDto, nodeParent, docId, i, entity);

            String type = entity.getString(FS_TYPE);
            if (VS_REQUEST.equals(type)) {
                // 设置接口学习
                TalendJsonToEntity.setApiInfo(menuApiDto, entity, node, spaceId);

                // 设置历史示例
                TalendJsonToEntity.setApiParamHis(menuApiDto, entity, node);
            }
            uploadMenuNodeFromJsonArray(param.getJSONArray(FS_CHILDREN), menuApiDto, node, spaceId, docId);
        }
    }

}
